package main;

/*
IDEAS: 
1) Let user specify desired SQL server adress
2) Create methods needed for electronic parts
*/



import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import javax.swing.JTable;



public class DBConnect {
	private Connection con;
	private Statement st;
	private ResultSet rs;
	
	// Contructor connects to set adress - if failed prints message
	public DBConnect(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("driver loaded");
			
			// -- set adress --
			//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chatdata", "root", "kubasik1");
			con = DriverManager.getConnection("jdbc:mysql://192.168.1.100:3306/soucastky", "root", "kubasik1");
			st = con.createStatement();
			System.out.println("Connected to Database");
			
		}catch(Exception ex){
			System.out.println("Erro: " + ex);
		}
		
	}
	// writes chat message to database
	public void setData(){
		try{
			// get time of sending
			Calendar calendar = Calendar.getInstance();
		    java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
		    
		    
			
			// prepare statement
			String query = " insert into chathistory " + " values (?,?)";
			
			PreparedStatement preparedStmt = con.prepareStatement(query);
			preparedStmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
			preparedStmt.setString(2, "Hello");
			
			// send statement
			preparedStmt.execute();
			
		}catch(Exception ex){
			System.out.println(ex);
		}
	}
	
	
	
	
	
	
	// method retrieves records and prints them out - depends on target structure - shoud receive desired location and desired data
	public JTable getData(String table){
		try{
			String query = "select * from " + table;
			rs = st.executeQuery(query);
			
			List<List<Integer>> components = new ArrayList<>();
			List<Integer> singleItem;
			// while cycle while there are more elements
			while(rs.next()){
				singleItem = new ArrayList<>();
				Integer value = rs.getInt(1);
				Integer tolerance = rs.getInt(2);
				Integer amount = rs.getInt(3);
				
				singleItem.add(value);
				singleItem.add(tolerance);
				singleItem.add(amount);
				
				components.add(singleItem);
			}
			
			System.out.println(components);

			Object[] compArray = components.toArray();
			Object[][] twoDArr = new Object[components.size()][3];
			List rowList;
			
			for (int i = 0; i < compArray.length; i++) {
				rowList = (ArrayList) (compArray[i]);
				twoDArr[i] = rowList.toArray();
			}
			
			/*
			 * table bude atribut DBConnect a v MainWindow se 1x vytvori instance DBConnect
			 * Pomoci gettru, lze ziskat table
			 * 
			 * table se bude v DBConnect v pripade potreby aktualizovat, modifikovat
			 * */
			
			
			
			Object[] colNames = {"value","tolerance","amount"};
			//List<String> columnNames = new ArrayList<String>(Arrays.asList(colNames));	
				
			return new JTable(twoDArr, colNames);
			
		}catch(Exception ex){
			System.out.println(ex);
		}
		return null;
	}
	
	
}
